/***************************************************************************
 *            udpclient.c
 *
 *  Thu May 31 09:08:42 2007
 *  Copyright  2007  kf701
 *  Email <kf701.ye AT gmail.com>
 ****************************************************************************/


#include "factory.h"

#define SA struct sockaddr

static int send_count;

int main(int argc,char **argv)
{
	debug_verbose = 4;

	if( argc < 3 )
	{
		sys_err("Usage: udpclient.out ip port\n");
		return 1;
	}

	char *ip = argv[1];
	uint16_t port = atoi(argv[2]);
	
	int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if ( sock == -1 )
	{
		sys_err("socket error\n");
		return -1;
	}
	
	struct hostent *hent;
	hent = gethostbyname(ip);
	if (NULL == hent) 
	{
		sys_err("gethostbyname err\n");
		close(sock);
		return -1;
	}
	struct sockaddr_in servaddr;
	memset((char*) &(servaddr),0, sizeof((servaddr)));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(port);
	servaddr.sin_addr = *(struct in_addr *)hent->h_addr;
	socklen_t len = sizeof(servaddr);
		
	char buf[256];
	sprintf(buf, "%s", argv[3]);
	int ret = sendto( sock, buf, sizeof(buf), 0, (SA*)&servaddr, len);
	if( ret != sizeof(buf) )
	{
		sys_err("sendto err\n");
		close( sock );
		return -1;
	}
	
	sys_log("send ok\n");
	close( sock );
	return 0;
}
